package com.wyp.didishige.fragment;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.umeng.fb.FeedbackAgent;
import com.umeng.update.UmengUpdateAgent;
import com.wyp.didishige.MyApplication;
import com.wyp.didishige.activity.ContactActivity;
import com.wyp.didishige.activity.users.BlackListActivity;
import com.wyp.didishige.utils.DataCleanManager;
import com.wyp.didishige.utils.StringUtils;
import com.wyp.didishige.utils.ToastUtil;
import com.wyp.didishige.R;
import com.wyp.didishige.activity.AboutActivity;
import com.wyp.didishige.activity.users.ContactUsersActivity;
import com.wyp.didishige.activity.users.FollowUserActivity;
import com.wyp.didishige.activity.GirlDynamicActivity;
import com.wyp.didishige.activity.users.InviteUsersActivity;
import com.wyp.didishige.activity.UserInfoActivity;
import com.wyp.didishige.activity.NotificationSettingActivity;
import com.wyp.didishige.activity.VIPActivity;
import com.wyp.didishige.config.Config;
import com.wyp.didishige.model.User;
import com.wyp.didishige.utils.ImageLoadOptions;
import com.wyp.didishige.utils.IntentUtil;

import cn.bmob.im.db.BmobDB;

/**
 * 设置
 *
 * @author horry
 * @ClassName: SetFragment
 * @date 2015-4-7 下午1:00:27
 */
@SuppressLint("SimpleDateFormat")
public class SettingsFragment extends FragmentBase implements OnClickListener {

    LinearLayout layout_notification, layout_update, layout_fb,layout_vip;
    private static final String ARG_SECTION_NUMBER = "section_number";

    ImageView user_head;
    TextView user_nick;

    TextView invite_num;
    TextView follow_num;
    TextView contact_num;
    TextView mCacheSize;
    TextView publish_community;

    private ImageView newContactTips=null;
    protected ImageView user_gender;

    /**用户信息*/
    protected LinearLayout layout_info;
    protected LinearLayout layout_contact;
    protected LinearLayout layout_about;
    protected LinearLayout layout_clear;
    protected LinearLayout layout_blacklist;
    protected LinearLayout contact_lay;
    protected LinearLayout follow_lay;
    protected LinearLayout invite_lay;

    User user;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        user = userManager.getCurrentUser(User.class);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_set, container, false);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initView();
        initData();
    }

    private void initView() {

        layout_notification = (LinearLayout) findViewById(R.id.layout_notification);
        layout_update = (LinearLayout) findViewById(R.id.layout_update);
        layout_fb = (LinearLayout) findViewById(R.id.layout_fb);
        layout_vip = (LinearLayout) findViewById(R.id.layout_vip);
        layout_info = (LinearLayout) findViewById(R.id.layout_info);
        layout_contact = (LinearLayout) findViewById(R.id.layout_contacts);
        layout_about = (LinearLayout) findViewById(R.id.layout_about);
        layout_clear = (LinearLayout) findViewById(R.id.layout_clear);
        layout_blacklist = (LinearLayout) findViewById(R.id.layout_blacklist);
        newContactTips = (ImageView) findViewById(R.id.newContactTips);

        contact_lay = (LinearLayout) findViewById(R.id.contact_lay);
        follow_lay = (LinearLayout) findViewById(R.id.follow_lay);
        invite_lay = (LinearLayout) findViewById(R.id.invite_lay);


        user_head = (ImageView) findViewById(R.id.user_head);
        user_nick = (TextView) findViewById(R.id.user_nick);
        publish_community = (TextView) findViewById(R.id.publish_community);
        mCacheSize = (TextView) findViewById(R.id.mCacheSize);

        invite_num = (TextView) findViewById(R.id.invite_num);
        follow_num = (TextView) findViewById(R.id.follow_num);
        contact_num = (TextView) findViewById(R.id.contact_num);

        layout_update.setOnClickListener(this);
        layout_fb.setOnClickListener(this);
        layout_notification.setOnClickListener(this);
        layout_vip.setOnClickListener(this);
        layout_info.setOnClickListener(this);

        invite_lay.setOnClickListener(this);
        follow_lay.setOnClickListener(this);
        contact_lay.setOnClickListener(this);
        layout_contact.setOnClickListener(this);
        layout_about.setOnClickListener(this);
        layout_clear.setOnClickListener(this);
        layout_blacklist.setOnClickListener(this);
    }

    private void initData() {
        ImageLoader.getInstance().displayImage(user.getAvatar(), user_head, ImageLoadOptions.getOptions());
        user_nick.setText(user.getNick());

        if (user.getInvites()!=null){
            invite_num.setText(String.format("%d", user.getInvites().size()));
        }else{
            invite_num.setText(String.format("%d",0));
        }

        if (user.getFollows()!=null){
            follow_num.setText(String.format("%d", user.getFollows().size()));
        }else{
            follow_num.setText(String.format("%d",0));
        }

        if (MyApplication.getInstance().getContactList()!=null){
            contact_num.setText(String.format("%d", MyApplication.getInstance().getContactList().size()));
        }else{
            contact_num.setText(String.format("%d",0));
        }
        if (!StringUtils.isNullOrEmpty(user.getCommunity())){
            publish_community.setText(user.getCommunity());
            publish_community.setVisibility(View.VISIBLE);
        }else{
            publish_community.setVisibility(View.GONE);
        }
        getCache();
    }

    private void getCache(){
        try {
            String cacheSize=DataCleanManager.getTotalCacheSize(getActivity());
            if (StringUtils.isNullOrEmpty(cacheSize)){
                mCacheSize.setText(cacheSize);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    @Override
    public void onResume() {
        super.onResume();
        if (BmobDB.create(getActivity()).hasNewInvite()){
            newContactTips.setVisibility(View.VISIBLE);
        }else{
            newContactTips.setVisibility(View.GONE);
        }
    }

    @Override
    public void onClick(View v) {
        Bundle bundle=null;
        switch (v.getId()) {

            case R.id.layout_info:
                Intent intent;
                intent = new Intent(getActivity(), UserInfoActivity.class);
                intent.putExtra(Config.KEY_USER_ID, userManager.getCurrentUser(User.class).getObjectId());
                intent.putExtra(Config.KEY_FROM, "me");
                startActivity(intent);
                break;
            case R.id.layout_update:
                UmengUpdateAgent.setUpdateOnlyWifi(false);
                UmengUpdateAgent.update(getActivity());
                break;
            case R.id.layout_notification:// 启动到个人资料页面
                IntentUtil.start_activity(getActivity(), NotificationSettingActivity.class);

                break;
            case R.id.layout_about:
                IntentUtil.start_activity(getActivity(), AboutActivity.class);
                break;
            case R.id.layout_vip:
                IntentUtil.start_activity(getActivity(), VIPActivity.class);
                break;
            case R.id.dynamic_lay:
                bundle = new Bundle();
                bundle.putSerializable("user", user);
                IntentUtil.start_activity(getActivity(), GirlDynamicActivity.class, bundle);
                break;
            case R.id.invite_lay:
                IntentUtil.start_activity(getActivity(), InviteUsersActivity.class);
                break;
            case R.id.follow_lay:
                IntentUtil.start_activity(getActivity(), FollowUserActivity.class);
                break;
            case R.id.contact_lay:
                IntentUtil.start_activity(getActivity(), ContactUsersActivity.class);
                break;
            case R.id.layout_contacts:
                IntentUtil.start_activity(getActivity(), ContactActivity.class);
                break;
            case R.id.layout_clear:
                DataCleanManager.cleanInternalCache(getActivity());
                getCache();
                ToastUtil.showShort(getActivity(),"清除缓存成功");
                break;
            case R.id.layout_blacklist:
                IntentUtil.start_activity(getActivity(), BlackListActivity.class);
                break;
            case R.id.layout_fb:
                FeedbackAgent agent = new FeedbackAgent(getActivity());
                agent.startFeedbackActivity();
                break;

        }
    }


}
